﻿using Vav.Data.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Vav.Data.TransferTypes;
using Vav.Data.Utils;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using CarPool.Data.Models;

namespace Vav.Data.DataAccess
{
    public class AccountDataAccess
    {
        private readonly VavdbContext vavdbContext;
        private readonly IMemoryCache _memoryCache;
        private readonly WeChatDataAccess weChatDataAccess;
        private readonly QQDataAccess  qQDataAccess;
        public AccountDataAccess(VavdbContext vavdbContext,
            IMemoryCache memoryCache,
            WeChatDataAccess weChatDataAccess,
            QQDataAccess qQDataAccess)
        {
            this.vavdbContext = vavdbContext;
            this._memoryCache = memoryCache;
            this.weChatDataAccess = weChatDataAccess;
            this.qQDataAccess = qQDataAccess;
        }

        [Description("判断用户是否存在")]
        public SysUser CheckUserExist(string unionid)
        {
            var User = vavdbContext.SysUser.FirstOrDefault(
                u => (u.UnionId == unionid) && u.DataStatus == 0);
            return User;
        }
        //微信用户登陆和注册
        public async Task<SysUser> WxLogin(string code, string appid, string iv, string encryptedData)
        {
            var data = await weChatDataAccess.GetWeChatProUserInfo(appid, code, encryptedData, iv);
            if (data != null)
            {
                var user = vavdbContext.SysUser.FirstOrDefault(u => u.UnionId.Equals(data.openid) && u.DataStatus == 0);
                if (user == null)
                {
                    //创建用户并且保存unionid 以及用户信息
                    SysUser sysUser = new SysUser
                    {
                        NickName = Regex.Replace(data.nickName ?? "", @"\p{Cs}", ""),
                        UnionId = data.openid,
                        Gender = data.gender,
                        HeadPic=data.avatarUrl,  
                    };
                    vavdbContext.Add(sysUser);
                    vavdbContext.SaveChanges();
                    //保存opneid

                    //vavdbContext.SaveChanges();
                    return sysUser;
                }
                else
                {
                    return user;
                }
            }
            return null;
        }

        public async Task<SysUser> QQLogin(string code, string appid, string iv, string encryptedData)
        {
            var data = await qQDataAccess.GetWeChatProUserInfo(appid, code, encryptedData, iv);
            if (data != null)
            {
                var user = vavdbContext.SysUser.FirstOrDefault(u => u.UnionId.Equals(data.openid) && u.DataStatus == 0);
                if (user == null)
                {
                    //创建用户并且保存unionid 以及用户信息
                    SysUser sysUser = new SysUser
                    {
                        NickName = Regex.Replace(data.nickName ?? "", @"\p{Cs}", ""),
                        UnionId = data.openid,
                        Gender = data.gender,
                        HeadPic = data.avatarUrl,
                    };
                    vavdbContext.Add(sysUser);
                    vavdbContext.SaveChanges();
                    //保存opneid

                    //vavdbContext.SaveChanges();
                    return sysUser;
                }
                else
                {
                    return user;
                }
            }
            return null;
        }


        public SysUser GetSysUser(int id)
        {
            var model = vavdbContext.SysUser.FirstOrDefault(u => u.Id == id && u.DataStatus == 0);
            return model;
        }
        public int UpdateSysUser(SysUser user)
        {
            vavdbContext.Update(user);
            return vavdbContext.SaveChanges();
        }

    }
}
